<?php


namespace Packages\Log\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Packages\Log\Services\LogService;
use Illuminate\Http\Request;

class LogController extends Controller
{
    protected LogService $logService;

    public function __construct(LogService $logService)
    {
        $this->logService = $logService;
    }

    /**
     * 列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'app_id' => 'string|max:32',
            'user_id' => 'string|size:32',
            'path' => 'string|max:255',
            'method' => 'string|max:12',
            'ip' => 'string|max:32',
            'url' => 'string|max:128',
            'obj_id' => 'string|max:32',
            'log_time' => 'array',
            'current' => 'integer|between:1,10000',
            'pageSize' => 'integer|between:1,1000',
        ];

        // 验证参数
        $params = $this->validateParams($request, $paramRule);

        $res = $this->logService->list($params);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }

    /**
     * 详情
     * @param Request $request
     * @return JsonResponse
     */
    public function detail(Request $request)
    {
        // 定义业务参数验证规则
        $paramRule = [
            'guid' => 'required|string|size:32',
        ];

        // 验证参数
        $params = $this->validateParams($request, $paramRule);

        $res = $this->logService->detail($params['guid']);

        if ($res['status']) {

            return $this->returnResponse($res['data']);
        }
        return $this->returnErrorResponse($res['data'], $res['code']);
    }
}
